<?php

###########################################################################
#   Copyright (C) 2012 CDNPAL LTD by Christopher Gu                       #
#   resources@cdnpal.com                                                  #
#                                                                         #
#   coded for Php 4.3/5.X and above                                       #
#   and AllowOverride all for .htaccess                                   #
#                                                                         #
#   This program is free software released                                #
#   under the GPL version 2 license:                                      #
#   http://www.gnu.org/licenses/gpl-2.0.htm                               #
#                                                                         #
#   This program is distributed WITHOUT ANY WARRANTY                      #
#   or FITNESS FOR A PARTICULAR PURPOSE.                                  #
#                                                                         #
#   DADF-SDK                                                              #
#                                                                         #
###########################################################################

class OutgoingOfferTransformer extends OutgoingTransformer {
	
	public $xmlData;
	
	public function __construct($offer_response_id_array, $table_direction = 'Outgoing') {
		
		$member_direction = 'outgoing';
		
		if ($table_direction == 'Incoming'):
			$offer_response_factory = IncomingOfferResponseFactory::get_instance();
			$member_direction = 'incoming';
		else:
			$offer_response_factory = OutgoingOfferResponseFactory::get_instance();
		endif;

		if (!is_array($offer_response_id_array)):
			
			$offer_response_id_array = array($offer_response_id_array);
			
		endif;
		
   		$dom_document = new DomDocument("1.0", "ISO-8859-1");

		$dom_element_dadf_offer 	= $dom_document->createElement('dadf-offer');

		$dom_element_responses 				= $dom_document->createElement('responses');
		
		foreach ($offer_response_id_array as $offer_response_id):
		
	    	$params[$table_direction . "OfferResponseID"] = $offer_response_id; 
	    	$offer_response = $offer_response_factory->get_row($params);
	    	
			$dom_element_response 			= $dom_document->createElement('response');
			
			$dom_element_accept				= $dom_document->createElement('accept', $this->to_boolean($offer_response->Accept));
			$dom_element_response->appendChild($dom_element_accept);
			$dom_element_id					= $dom_document->createElement('id', $offer_response->ID);
			$dom_element_response->appendChild($dom_element_id);
			$dom_element_price				= $dom_document->createElement('offertype', $offer_response->OfferType);
			$dom_element_response->appendChild($dom_element_price);
			$dom_element_price				= $dom_document->createElement('price', $offer_response->Price);
			$dom_element_response->appendChild($dom_element_price);
			
			if (!is_null($offer_response->CashDeal)):
				$dom_element_cashdeal		= $dom_document->createElement('cashdeal', $offer_response->CashDeal);
				$dom_element_response->appendChild($dom_element_cashdeal);
			endif;
			
			if (!is_null($offer_response->PauseTrigger)):
				$dom_element_pausetrigger	= $dom_document->createElement('pausetrigger', $offer_response->PauseTrigger);
				$dom_element_response->appendChild($dom_element_pausetrigger);
			endif;
			
			if (!is_null($offer_response->ResumeTrigger)):
				$dom_element_resumetrigger	= $dom_document->createElement('resumetrigger', $offer_response->ResumeTrigger);
				$dom_element_response->appendChild($dom_element_resumetrigger);
			endif;
			
			if (!is_null($offer_response->MinutesToExpire)):
				$dom_element_minutestoexpire = $dom_document->createElement('minutestoexpire', $offer_response->MinutesToExpire);
				$dom_element_response->appendChild($dom_element_minutestoexpire);
			endif;
			
			$dom_element_message			= $dom_document->createElement('message', $offer_response->Message);
			$dom_element_response->appendChild($dom_element_message);
			$dom_element_acceptid			= $dom_document->createElement('acceptid', $offer_response->AcceptID);
			$dom_element_response->appendChild($dom_element_acceptid);
			$dom_element_partnerid			= $dom_document->createElement('partnerid', $offer_response->PartnerID);
			$dom_element_response->appendChild($dom_element_partnerid);
			$dom_element_partnername		= $dom_document->createElement('partnername', $offer_response->PartnerName);
			$dom_element_response->appendChild($dom_element_partnername);

			$dom_element_responses->appendChild($dom_element_response);
			
		endforeach;
		
		$dom_element_dadf_offer->appendChild($dom_element_responses);
		$dom_document->appendChild($dom_element_dadf_offer);
		$dom_document->formatOutput = true;
		
		$dadf_version = Zend_Registry::get('application.version');
		
		if ($dom_document->schemaValidate ('../application/schemas/Dadf.offer-' . $dadf_version . '.xsd') == false):
			
			throw new Exception("DADF schema validation failed");
			
		endif;
		
		$this->xmlData  = $dom_document->saveXML();
	

	}		
	
}
			